//
//  StatusViewModel.swift
//  WBSwift
//
//  Created by maker on 2016/11/16.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    var status : statusModel?               //状态模型
    var rowHeight : CGFloat = 0.0           //行高
    
    var sourceText : String?                //来源
    var timeText : String?                  //发表时间
    var verifyImage : UIImage?              //认证图片
    var vipImage : UIImage?                 //vip图片
    var imaURLs : [URL]? = [URL]()      //配图数组
    
    init(status: statusModel) {
        super.init()
        self.status = status
        
//MARK:==========初始化 数据处理 ==========
        //1.处理来源
        if let source = status.source, source != "" {
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            sourceText = (source as NSString).substring(with: NSRange(location: startIndex, length: length))
            sourceText = "来自" + sourceText!
        }
        
        //2.处理时间
        if let created_at = status.created_at, created_at != "" {
            timeText = Date.getCreatTime(created_at)
        }
        
        //3.处理用户认证
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
            case 0:
                verifyImage = UIImage(named: "avatar_vip")
            case 2,3,5:
                verifyImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifyImage = UIImage(named: "avatar_grassroot")
            default:
                verifyImage = nil
        }
        
        //4.处理会员等级
        let mbrank = status.user?.mbrank ?? 0
        
        if (mbrank>0) && (mbrank<7) {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        //5.处理配图,(图片的数据来自两个方向1.原创微博的图,或者转发的微博的图)
        var picDicts = [[String :String]]()
        if status.pic_urls.count > 0 {
             picDicts = status.pic_urls
        }else{
            if let retweet = status.retweeted_status {
                picDicts = retweet.pic_urls
            }
        }
        
        for dict in picDicts {
            guard let urlString = dict["thumbnail_pic"] else{
                continue
            }
            imaURLs?.append(URL(string: urlString)!)
        }
       
        

    }
}
